package cn.anecansaitin.firecrafting.common.item;

import cn.anecansaitin.firecrafting.common.table.ModCreateTable;
import net.minecraft.advancements.CriteriaTriggers;
import net.minecraft.core.BlockPos;
import net.minecraft.core.Direction;
import net.minecraft.network.chat.Component;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.sounds.SoundEvents;
import net.minecraft.sounds.SoundSource;
import net.minecraft.world.InteractionResult;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.Item;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.Rarity;
import net.minecraft.world.item.TooltipFlag;
import net.minecraft.world.item.context.UseOnContext;
import net.minecraft.world.item.crafting.RecipeType;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.BaseFireBlock;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.level.gameevent.GameEvent;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.List;

public class Lighter extends Item {
    private final BaseFireBlock fire;
    private final List<Component> tips;

    public Lighter(BaseFireBlock fire, @Nullable Rarity rarity, List<Component> tips) {
        super(new Properties()
                .defaultDurability(64)
                .rarity(rarity == null ? Rarity.COMMON : rarity)
                .tab(ModCreateTable.TAB));
        this.fire = fire;
        this.tips = tips;
    }

    @Override
    public @NotNull InteractionResult useOn(@NotNull UseOnContext context) {
        Player player = context.getPlayer();
        Level world = context.getLevel();
        BlockPos blockpos = context.getClickedPos();
        //判断是否点击方块顶部
        if (context.getClickedFace() == Direction.UP && !(world.getBlockState(blockpos).getBlock() instanceof BaseFireBlock)) {
            //获取上方的方块
            BlockPos above = blockpos.above();
            BlockState aboveState = world.getBlockState(above);
            if (aboveState.isAir()) {
                world.playSound(player, above, SoundEvents.FLINTANDSTEEL_USE, SoundSource.BLOCKS, 1.0F, world.getRandom().nextFloat() * 0.4F + 0.8F);
                BlockState fireState = fire.defaultBlockState();
                world.setBlock(above, fireState, 11);
                world.gameEvent(player, GameEvent.BLOCK_PLACE, blockpos);
                ItemStack itemstack = context.getItemInHand();
                if (player instanceof ServerPlayer) {
                    CriteriaTriggers.PLACED_BLOCK.trigger((ServerPlayer) player, above, itemstack);
                    itemstack.hurtAndBreak(1, player, (player1) -> player1.broadcastBreakEvent(context.getHand()));
                }

                return InteractionResult.sidedSuccess(world.isClientSide());
            } else {
                return InteractionResult.FAIL;
            }
        }
        //返回跳过
        return InteractionResult.PASS;
    }

    @Override
    public void appendHoverText(ItemStack stack, @Nullable Level world, List<Component> tips, TooltipFlag flag) {
        tips.addAll(this.tips);
    }

    @Override
    public int getBurnTime(ItemStack itemStack, @Nullable RecipeType<?> recipeType) {
        return super.getBurnTime(itemStack, recipeType);
    }
}
